﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Medianamik.Core
{
    public static class PersistenceMethods
    {
        public const string Create = "Create";
        public const string Read = "Read";
        public const string Update = "Update";
        public const string Delete = "Delete";
    }

    public static class PlatformNames
    {
        public const string CSharp = "C-Sharp";
        public const string ObjectiveC = "Objective-C";
        public const string CSharpWp7 = "C-Sharp WP7";
        public const string JavaAndroid = "Java Android";
    }

    public class NodeTypeExtendedProperties
    {
        public const string ROOT_ELEMENT_NAME = "ExtendedProperties";
        public const string PROPERTY_POSITIONS_ELEMENT_NAME = "PropertyPositions";
        public const string PROPERTY_GENERATION_ELEMENT_NAME = "GenerationPlatforms";
        public const string CLIENT_TEMPLATE_PATHS_ELEMENT_NAME = "ClientTemplatePaths";
        public const string HIDDEN_PROPERTIES_ELEMENT_NAME = "HiddenProperties";

        internal NodeTypeExtendedProperties(string pDocument)
        {
            Document = ParseDocument(pDocument);
        }

        internal NodeTypeExtendedProperties()
        {
            Document = ParseDocument(null);
        }

        public XDocument Document { get; private set; }

        internal XElement PropertyPositions
        {
            get
            {
                var elm = Document.Root.Element(PROPERTY_POSITIONS_ELEMENT_NAME);
                if (elm == null)
                {
                    elm = new XElement(PROPERTY_POSITIONS_ELEMENT_NAME);
                    Document.Root.Add(elm);
                }
                return elm;
            }
        }

        public XElement GenerationPlatforms
        {
            get
            {
                var elm = Document.Root.Element(PROPERTY_GENERATION_ELEMENT_NAME);
                if (elm == null)
                {
                    elm = new XElement(PROPERTY_GENERATION_ELEMENT_NAME);
                    Document.Root.Add(elm);
                }
                return elm;
            }
        }

        internal XElement HiddenProperties
        {
            get
            {
                var elm = Document.Root.Element(HIDDEN_PROPERTIES_ELEMENT_NAME);

                if (elm == null)
                {
                    elm = new XElement(HIDDEN_PROPERTIES_ELEMENT_NAME);
                    Document.Root.Add(elm);
                }

                return elm;
            }
        }

        internal XElement GetPropertyPositionElement(Guid pPropertyId)
        {
            var positionElm = PropertyPositions.Elements().FirstOrDefault(elm =>
                {
                    var attr = elm.Attribute("Id");
                    return attr == null ? false : (Guid)attr == pPropertyId;
                });

            return positionElm;
        }

        internal XElement GetPropertyHiddenElement(Guid propertyId)
        {
            var positionElm = HiddenProperties.Elements().FirstOrDefault(elm =>
            {
                var attr = elm.Attribute("Id");
                return attr == null ? false : (Guid)attr == propertyId;
            });

            return positionElm;
        }

        public XElement ClientTemplatePaths
        {
            get
            {
                var elm = Document.Root.Element(CLIENT_TEMPLATE_PATHS_ELEMENT_NAME);
                if (elm == null)
                {
                    elm = new XElement(CLIENT_TEMPLATE_PATHS_ELEMENT_NAME);
                    Document.Root.Add(elm);
                }
                return elm;
            }

        }

        public XElement Icon
        {
            get
            {
                var elm = Document.Root.Element("Icon");
                if (elm == null)
                {
                    elm = new XElement("Icon");
                    Document.Root.Add(elm);
                }
                return elm;
            }
        }

        private static XDocument ParseDocument(string pDocument)
        {
            if (string.IsNullOrEmpty(pDocument))
            {
                return new XDocument(new XElement(ROOT_ELEMENT_NAME));
            }
            else
            {
                return XDocument.Parse(pDocument);
            }
        }
    }
}
